/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.ufrgs.inf.prav.interop.jsf.components;

import java.io.IOException;
import java.util.List;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import obai.control.PlataformBean.Plataform;
import obai.model.util.PathParser;

/**
 *
 * @author Fernando Arena Varella
 * @version 1.0
 */
public class VideoComponent extends PravBaseComponent {
    
    public static final String COMPONENT_FAMILY = "PRAV_VIDEO";
    public static final String COMPONENT_TYPE = "PRAV_VIDEO";

    //attributes
    private String height, posterHref, width, scale;
    private boolean autoplay = false, autoHref = false, controller = true;
    private String mobileHref, tvdHref, webHref;
    private String scriptPath;



    public VideoComponent(){

    }
    
    @Override
    public void encodeBegin(FacesContext context) throws IOException {
//        super.setDisplayMobile(false);
//        super.setDisplayTv(false);
//        super.setDisplayWeb(false);
        if (!displayPlataform()){
            hideInnerComponents();
            return;
        }

		ResponseWriter writer =
			context.getResponseWriter();

        //if mobileHref is set and targetPlataform is MOBILE
        if ((plataformBean.getPlataform() == Plataform.MOBILE) &&
                (mobileHref != null)){
//            super.setDisplayMobile(true);
            startMobileVideo(context);
        }

        if ((plataformBean.getPlataform() == Plataform.DIGITALTV) &&
                (tvdHref != null))
           buildTvVideo(writer);

        if ((plataformBean.getPlataform() == Plataform.WEB) &&
                (webHref != null))
            buildWebVideo(writer);


        //fix src attribute


//        hidePlataformSpecificContent(writer);
        writer.flush();
	}

    @Override
    public void encodeEnd(FacesContext context)
            throws IOException {

        if ((plataformBean.getPlataform() == Plataform.MOBILE) &&
                (mobileHref != null)){
            //close <a> tag
            endMobileVideo(context);
        }
    }

    @Override
    public void decode(FacesContext context) {
        return;
    }


    //util methods
    private void startMobileVideo(FacesContext context) throws IOException{
        AComponent aComponent = new AComponent();
        aComponent.setHref(mobileHref);
        aComponent.setDisplayMobile(displayPlataform());
        aComponent.encodeBegin(context);

        List<UIComponent> kids = this.getChildren();
        if (kids.size() < 1)
            context.getResponseWriter().write("Video");

//        writer.startElement("a", this);
//        writer.writeAttribute("href", mobileHref, "href");
//        if (title != null)
//            writer.write(title);
//        else
//            writer.write(getValue());
//        writer.endElement("a");
    }
    private void endMobileVideo(FacesContext context) throws IOException{
        if (!displayPlataform())
            return;
        ResponseWriter writer = context.getResponseWriter();
        writer.endElement("a");
        writer.flush();
    }
    
    private void buildTvVideo(ResponseWriter writer){
    }
    private void buildWebVideo(ResponseWriter writer) throws IOException{
        String CRLF = "\n\t\t\t";

//        src=\"/AC_QuickTime.js\"
        hideInnerComponents();

        //hack to overcome posterHref null bug
        posterHref = (posterHref != null) ? posterHref : "";

        if (scriptPath == null)
            scriptPath = "AC_Quicktime.js";
        StringBuilder sb = new StringBuilder();
        sb.append("<script src=\"" + scriptPath +
                "\" language=\"JavaScript\" type=\"text/javascript\"></script>\n\t\t");
        sb.append("<script language=\"JavaScript\" type=\"text/javascript\">\n\t\t");
        sb.append("QT_WriteOBJECT(");

        Attribute href = new Attribute();
        href.value = webHref;

        buildParams(href);
        
        //fix the height if the controller equals true
        try {
            int h = Integer.parseInt(height);
            h += 16;
            height = String.valueOf(h);
        } catch (Exception e) {
        }

        sb.append("'" + posterHref + "',");
        sb.append(width != null ? "'" + width + "'," : "'',");
        //append height + 'ActiveX Version' + newLine and tabs
        sb.append(height != null ? "'" + height + "',''," + CRLF : "'',''," + CRLF);

        sb.append("'" + href.name + "','" + href.value + "'," + CRLF);
        sb.append("'TYPE','video/quicktime'," + CRLF);

        sb.append("'autohref','" + autoHref + "'," + CRLF);
        sb.append("'autoplay','" + autoplay + "'," + CRLF);
        sb.append("'controller','" + controller + "'," + CRLF);

        sb.append("'target','myself');");
        sb.append("\n\t\t</script>");

        writer.write(sb.toString());
    }

    private void buildWebVideo2(ResponseWriter writer) throws IOException{
        String CRLF = "\n\t\t\t";

//        src=\"/AC_QuickTime.js\"
        hideInnerComponents();

        StringBuilder sb = new StringBuilder();
        sb.append("<script src=\"" + scriptPath +
                "\" language=\"JavaScript\" type=\"text/javascript\"></script>\n\t\t");
        sb.append("<script language=\"JavaScript\" type=\"text/javascript\">\n\t\t");
        sb.append("QT_WriteOBJECT(");

        boolean pathContainsProtocol = PathParser.pathContainsProtocol(webHref);

        if (!pathContainsProtocol)
            sb.append(posterHref != null ? "'" + posterHref + "'," : "'',");
        else
            sb.append("'',");

        //fix the height if the controller equals true
        if (controller)
            height += 16;

        sb.append(width != null ? "'" + width + "'," : "'',");
        //append height + 'ActiveX Version' + newLine and tabs
        sb.append(height != null ? "'" + height + "',''," + CRLF : "'',''," + CRLF);
        
        if (pathContainsProtocol){
            sb.append("'qtsrc','" + webHref + "'," + CRLF);
            sb.append("'TYPE','video/quicktime'," + CRLF);
        }


        //apend autohref + newLine;
        if (autoHref)
            sb.append("'autohref','true'," + CRLF);
        else
            sb.append("'autohref','false'," + CRLF);
        //apend controller + newLine;
        if (controller)
            sb.append("'controller','true'," + CRLF);
        else
            sb.append("'controller','false'," + CRLF);
        //apend autoplay + );
        if (autoplay)
            sb.append("'autoplay','true');");
        else
            sb.append("'autoplay','false');");

        sb.append("\n\t\t</script>");

        writer.write(sb.toString());
    }


    private class Attribute {
        public String name, value;
    }

    /**
     * Builds the parameters for the js method that builds the Web Video
     * Mode 1: Streaming video with poster href
     * Mode 2: Streaming video without poster href
     * Mode 3: Local video with poster href
     * Mode 4: Local video without poster href
     * @param posterHref
     * @param href
     */
    private void buildParams(Attribute href){
        if (PathParser.pathContainsProtocol(href.value)){
            //Mode 1 or 2
//            if (posterHref != null){
            if (!posterHref.equals("")){
                //Mode 1 - Streaming video with poster href
                href.name = "href";
            } else {
                //nao passou no == null quando é null
                //Mode 2 - Streaming video without poster href
                href.name = "qtsrc";
                posterHref = "";
            }
        } else {
            //Mode 3 or 4
            if (!posterHref.equals("")){
                //Mode 3 - Local video with poster href
                href.name = "href";
            } else {
                //Mode 4 - Local video without poster href
                posterHref = href.value;
                href.name = "";
                href.value = "";
            }
        }
    }

    public boolean isAutoHref() {
        return autoHref;
    }



    // <editor-fold defaultstate="collapsed" desc="Getters And Setters">
    public void setAutoHref(boolean autoHref) {
        this.autoHref = autoHref;
    }

    public boolean isAutoplay() {
        return autoplay;
    }

    public void setAutoplay(boolean autoplay) {
        this.autoplay = autoplay;
    }

    public boolean isController() {
        return controller;
    }

    public void setController(boolean controller) {
        this.controller = controller;
    }
    

    public String getHeight() {
        return height;
    }

    public void setHeight(String height) {
        this.height = height;
    }

    public String getPosterHref() {
        return posterHref;
    }

    public void setPosterHref(String posterHref) {
        this.posterHref = posterHref;
    }

    public String getMobileHref() {
        return mobileHref;
    }

    public void setMobileHref(String mobileHref) {
        this.mobileHref = mobileHref;
    }

    public String getTvdHref() {
        return tvdHref;
    }

    public void setTvdHref(String tvdHref) {
        this.tvdHref = tvdHref;
    }

    public String getWebHref() {
        return webHref;
    }

    public void setWebHref(String webHref) {
        this.webHref = webHref;
    }

    public String getWidth() {
        return width;
    }

    public void setWidth(String width) {
        this.width = width;
    }

    
    public String getScale() {
        return scale;
    }

    public void setScale(String scale) {
        this.scale = scale;
    }

    public String getScriptPath() {
        return scriptPath;
    }

    public void setScriptPath(String scriptPath) {
        this.scriptPath = scriptPath;
    }


    // </editor-fold>

}
